import {
  HOST, PORT, AUTH_USER, AUTH_PASS, EMAIL_FROM
} from '../../constants/email';

const nodemailer = require('nodemailer');

class Email {
  static transporter;

  static init() {
    // create reusable transporter object using the default SMTP transport
    const transporter = nodemailer.createTransport({
      host: HOST,
      port: PORT,
      secure: false, // upgrade later with STARTTLS
      auth: {
        user: AUTH_USER,
        pass: AUTH_PASS,
      },
    });

    this.transporter = transporter;
  }

  static async sendEmail(email: string, docxBuffer: Buffer) {
    const info = await this.transporter.sendMail({
      from: `职业心理健康状况调查"${EMAIL_FROM}"`, // sender address
      to: email, // list of receivers
      subject: '员工职业心理状况调查', // Subject line
      attachments: [{
        filename: '员工职业心理状况调查.docx',
        contentType: 'application/docx',
        content: docxBuffer
      }], // plain text body
    });

    return info;
  }
}

export default Email;
